package com.sctvcloud.zhaohua_sd.ui.adapter;

import android.content.Context;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ruihang.generalibrary.ui.adapter.BaseHolderAbsAdapter;
import com.ruihang.generalibrary.ui.util.IAndroidLittleLife;
import com.ruihang.generalibrary.ui.util.LittleLifeManager;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.sctvcloud.zhaohua_sd.R;
import com.sctvcloud.zhaohua_sd.base.BaseActivity;
import com.sctvcloud.zhaohua_sd.base.BaseFragment;
import com.sctvcloud.zhaohua_sd.ui.adapter.holder.BaseAbsHolder;
import com.sctvcloud.zhaohua_sd.ui.adapter.holder.LiveBannerHolder;
import com.sctvcloud.zhaohua_sd.ui.adapter.holder.LiveHomeAdvanceHolder;
import com.sctvcloud.zhaohua_sd.ui.adapter.holder.LiveHomeCountDownHolder;
import com.sctvcloud.zhaohua_sd.ui.adapter.holder.LiveHomeItemHolder;
import com.sctvcloud.zhaohua_sd.ui.adapter.holder.OnlyTitleHolder;
import com.sctvcloud.zhaohua_sd.ui.fragment.LiveFragment;
import com.sctvcloud.zhaohua_sd.ui.util.IListShowData;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liuyu on 2017/7/11.
 */

public class LiveHomeAdapter extends BaseHolderAbsAdapter<IListShowData, BaseAbsHolder<IListShowData>> {

    public static final int VIEWTYPE_ADVANCE_BANNER = 1;//预告banner
    public static final int VIEWTYPE_ADVANCE = 2;//预告
    public static final int VIEWTYPE_TITLE = 3;//标题
    public static final int VIEWTYPE_LIVE = 4;//直播的item
    public static final int VIEWTYPE_ADVANCE_LIST = 5;//直播预告列表

    private OnItemInternalClick onItemSubscribeClick;
    private LittleLifeManager littleLifeManager;
    private BaseActivity activity;
    private BaseFragment fragment;
    private ArrayList<Pair<String, String>> pairs;
    private LiveHomeItemHolder.LiveItemClickListener listener;

    public void setActivity(BaseActivity activity) {
        this.activity = activity;
    }

    public void setFragment(BaseFragment fragment) {
        this.fragment = fragment;
    }

    public void setPairs(ArrayList<Pair<String, String>> pairs) {
        this.pairs = pairs;
    }

    public OnItemInternalClick getOnItemSubscribeClick() {
        return onItemSubscribeClick;
    }

    public void setOnItemSubscribeClick(OnItemInternalClick onItemSubscribeClick) {
        this.onItemSubscribeClick = onItemSubscribeClick;
    }

    public void setListener(LiveHomeItemHolder.LiveItemClickListener listener) {
        this.listener = listener;
    }

    public LiveHomeAdapter(Context context, List<IListShowData> list ) {
        super(context, list);
        littleLifeManager = new LittleLifeManager();
    }

    @Override
    public BaseAbsHolder<IListShowData> onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseAbsHolder bah = null;
        View v = null;
        switch (viewType) {
            case VIEWTYPE_ADVANCE://预告
                v = LayoutInflater.from(context).inflate(R.layout.item_live_advance, parent, false);
                bah = new LiveHomeAdvanceHolder(context, v);
                bah.setInternalClick(onItemSubscribeClick);
                break;
            case VIEWTYPE_ADVANCE_LIST://预告
                v = LayoutInflater.from(context).inflate(R.layout.item_live_home_count_down, parent, false);
                bah = new LiveHomeCountDownHolder(context, v);
                bah.setPairs(pairs);
                bah.setFragment(fragment);
                bah.setActivity(activity);
                break;
            case VIEWTYPE_ADVANCE_BANNER://预告banner
                v = LayoutInflater.from(context).inflate(R.layout.item_live_banner, parent, false);
                bah = new LiveBannerHolder(context, v);
                bah.setPairs(pairs);
                bah.setFragment(fragment);
                bah.setActivity(activity);
                break;
            case VIEWTYPE_TITLE://标题
                v = LayoutInflater.from(context).inflate(R.layout.item_live_title, parent, false);
                bah = new OnlyTitleHolder(context, v);
                break;
            case VIEWTYPE_LIVE://直播
            default:
                v = LayoutInflater.from(context).inflate(R.layout.item_live_home_child, parent, false);
                bah = new LiveHomeItemHolder(context, v, listener,fragment);
                bah.setPairs(pairs);
                bah.setFragment(fragment);
                bah.setActivity(activity);
                break;
        }
        return bah;
    }

    @Override
    public int getItemViewType(int position) {
        return data.get(position).getDataViewType();
    }

    @Override
    public void onBindViewHolder(BaseAbsHolder<IListShowData> holder, int position) {
        super.onBindViewHolder(holder, position);
        holder.setItemMode(position == getItemCount() - 1 ? BaseAbsHolder.ITEMMODE_LAST : BaseAbsHolder.ITEMMODE_NORMAL);
    }

    @Override
    public void onResume() {
        littleLifeManager.onResume();
    }

    @Override
    public void onPause() {
        littleLifeManager.onPause();
    }

    @Override
    public void onDestory() {
        littleLifeManager.onDestory();
    }

    protected void addLife(IAndroidLittleLife ia) {
        littleLifeManager.addLifeInf(ia);
    }
}
